﻿define([
    this.path + './../4.x/plugins/esri/layers/ExGraphicsLayer.js', "esri/PopupTemplate"
], function(GraphicsLayer, PopupTemplate) {
    var PointLayer = GraphicsLayer.createSubclass({
        setData: function(mapView, layer) {
            var me = this;
            if (!mapView) {
                return
            };
            var mapUtils = mapView.mapUtils;
            var options = layer.layer || {};
            me.options = options;
            var data = options.data || [];
            var popupOptions = options.popupOptions;
            var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
            var textFieldName = options.textFieldName || mapView.getTextFieldName();
            var minScale = options.minScale;
            var maxScale = options.maxScale;
            if (minScale) {
                me.minScale = minScale;
            };
            if (maxScale) {
                me.maxScale = maxScale;
            }


            me.convertSr(mapView, lngLatArray, (data) => {
                data.forEach(function(lngLat, index) {
                    var currentSymbol = lngLat[symbolFieldName] || options.symbol || 'common_text';
                    mapUtils.getSymbol(mapView, currentSymbol, function(symbol) {
                        symbol.text = lngLat[textFieldName];
                        me.addGraphic(mapView, mapUtils, lngLat, symbol, popupOptions);
                        if (index == lngLatArray.length - 1) {
                            if (typeof options.callback == 'function') {
                                options.callback(layer);
                            };
                        };
                    });
                });
            })

        },
        addGraphic: function(mapView, mapUtils, lngLat, symbol, popupOptions) {
            var me = this;
            var point = mapUtils.createPoint(mapView, lngLat);
            var popupTemplate = null;
            if (popupOptions) {
                popupTemplate = new PopupTemplate();
                popupTemplate.title = popupOptions.getTitle(lngLat);
                popupTemplate.content = popupOptions.getContent(lngLat);
            };

            var graphic = mapUtils.createGraphic(mapView, {
                geometry: point,
                symbol: symbol,
                attributes: lngLat,
                popupTemplate: popupTemplate
            });
            me.add(graphic);
        },
        getGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            };
            if (!mapView) {
                return;
            };

            var map = mapView.getMap();
            if (!map) {
                return;
            };

            var geometry = esriEvent.graphic.geometry || {};
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        }
    });
    return PointLayer;
});